<?php

declare(strict_types=1);

namespace app\common\dao\sys;

use app\common\model\sys\SysUser;
use app\common\traits\DaoTrait;
use think\facade\Db;

/**
 * 系统用户 - DAO
 * @author:kaige
 * @update:2022-06-12
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class UserDao
{
  // 当前操作的模型
  protected $model = null;

  // 对应模型的主键，默认为id
  protected $pk = 'id';

  // 对应模型默认查询的字段
  protected $name = 'un';

  // 对应模型允许查询的字段(默认全部查询)
  protected $allowField = ['*'];

  // 允许单字段更新
  protected $uaField = ['status'];

  // 查询结果-要隐藏的字段 默认空数组
  protected $hideField = ['pwd', 'sz_salt'];

  public function __construct()
  {
    $this->model = new SysUser();
  }
  use DaoTrait;

  /**
   * 根据用户ID获取公司ID
   */
  public function getCompanyIdByUserId($uid)
  {
    return $this->model->where('id', $uid)->value('company_id');
  }

  /**
   * 查重
   */
  public function userExist($username)
  {
    return $this->model->withTrashed()->where(['un' => $username])->count() > 0;
  }

  /**
   * 查重
   */
  public function userExistKey($username, $key)
  {
    return $this->model->withTrashed()->where([$key => $username])->count() > 0;
  }

  /**
   * 获取员工列表
   * @return array
   */
  public function getEmployeeList()
  {
    return $this->model->field('id,real_name,real_name as name,phone')->where(['status' => 1])->order('sort asc')->select()->toArray(); // 整个name，个别片断需要
  }

  /**
   * 获取员工列表 -API用
   * @return array
   */
  public function getEmployeeListUseAPI()
  {
    return $this->model->field('id,real_name,real_name as name,avatar,dept_id,dept_id_name,sign,online,phone,pingyin_name')
      ->where([['status', '=', 1]])->order('pingyin_name asc,sort asc')->select()->toArray();
  }


  /**
   * 依ID获取用户姓名
   * @return string
   */
  public function getEmployeeName($id)
  {
    return $this->model->field('id,real_name as name,phone')->where(['status' => 1, 'id' => $id])->value('real_name');
  }
  /**
   * 依ID获取用户信息
   * @return array
   */
  public function getEmployeeInfo($id)
  {
    $data = $this->model->field('id,real_name as name,real_name,phone,dept_id,dept_id_name,company_id,avatar,entry_time,ranking_id_name,ranking_id')->where(['status' => 1, 'id' => $id])->find();
    if ($data) {
      return $data->toArray();
    }
    return [];
  }

  /**
   * 取根据部门下所有人员
   */
  public function getDeptUserList($dept_id = 0)
  {
    return $this->model->field('id,real_name,real_name as name,avatar,dept_id,dept_id_name,sign,online,phone,ranking_id_name,ranking_id')->where(['dept_id' => $dept_id, 'status' => 1])->select()->toArray();
  }

  /**
   * 根据部门ID集获取用户ID集
   */
  public function getUserIdByInDeptIds($dept_ids)
  {
    return $this->model->where([['dept_id', 'IN', $dept_ids], ['status', '=', 1]])->column('id');
  }

  /**
   * 根据公司ID获取用户ID集
   */
  public function getUserIdByCompanyId($company_id = 0)
  {
    $where = [];
    $where[] = ['company_id', '=', $company_id];
    $where[] = ['status', '=', 1];
    return $this->model->where($where)->column('id');
  }

  /**
   * 根据部门ID获取用户ID集
   */
  public function getUserIdByDeptId($dept_id = 0)
  {
    $where = [];
    $where[] = ['dept_id', '=', $dept_id];
    $where[] = ['status', '=', 1];
    return $this->model->where($where)->column('id');
  }

  /**
   * 按部门统计
   */
  public function getUserListByDept()
  {
    $list = $this->model->field('dept_id_name, count(id) as num')->where(['status' => 1])->group('dept_id')->select();
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按性别统计
   */
  public function getUserListBySex()
  {
    $list = $this->model->field('sex, count(sex) as num')->where(['status' => 1])->group('sex')->select();
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按年龄统计
   */
  public function getUserListByAge()
  {
    $prefix = env('database.prefix');
    $list = Db::query("SELECT YEAR(CURDATE()) - substr(id_card,7,4) as age, count(id) as num FROM " . $prefix . "sys_user WHERE status=1 group by substr(id_card,7,4)");
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按学历统计
   */
  public function getUserListByEdu()
  {
    $list = $this->model->field('educational, count(id) as num')->where(['status' => 1])->group('educational')->select();
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按职位统计
   */
  public function getUserListByRanking()
  {
    $list = $this->model->field('ranking_id_name, count(id) as num')->where(['status' => 1])->group('ranking_id_name')->select();
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按入职年份统计
   */
  public function getUserListByEntryYear()
  {
    $prefix = env('database.prefix');
    $list = Db::query("SELECT from_unixtime(entry_time,'%Y') as uyear, count(id) as num FROM " . $prefix . "sys_user WHERE status=1 group by from_unixtime(entry_time,'%Y')");
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 按入职年限统计
   */
  public function getUserListByEntryYn()
  {
    $prefix = env('database.prefix');
    $list = Db::query("SELECT YEAR(CURDATE()) - from_unixtime(entry_time,'%Y') as uyear, count(id) as num FROM " . $prefix . "sys_user group by YEAR(CURDATE()) - from_unixtime(entry_time,'%Y')");
    $count = $this->model->where(['status' => 1])->count();
    return _layui($count, $list);
  }

  /**
   * 获得下属统计总数
   */
  public function getUnderlingCount($user_id = 0)
  {
    return $this->model->where(['super_id' => $user_id])->count('id');
  }
  /**
   * 获得下属ids
   */
  public function getUnderlingIds($user_id = 0)
  {
    return $this->model->where(['super_id' => $user_id])->column('id');
  }


}
